package core

import (
	"image"
	"image/color"
	"log"
	"mime/multipart"
	"sort"

	_ "image/gif"
	_ "image/jpeg"
	_ "image/png"
)

// 滑块缺口坐标识别算法， 作者: 李漫;
// 原理: 相邻像素色差法 - 轴像素点对比差 （缺口部分一般为直线，与上一个像素色差大的规律可以获取到，缺口的坐标）

type kv struct {
	k int
	v int
}

type XY struct {
	X int `json:"x"`
	Y int `json:"y"`
}

type OutInfo struct {
	Width int `json:"width"`
	Height int `json:"height"`
	X1 int	`json:"x1"`
	X2 int	`json:"x2"`
	Y1 int	`json:"y1"`
	Y2 int	`json:"y2"`
	Coordinate []*XY	`json:"coordinate"`
}

func GetQueKou(source multipart.File) (*OutInfo, error) {

	out := &OutInfo{}

	//source := "./bg_7.png"

	//ff, err := ioutil.ReadFile(source) //读取文件
	//if err != nil {
	//	log.Println("图片打开失败 : ", err)
	//}
	//bbb := bytes.NewBuffer(ff)
	m, _, err := image.Decode(source)
	if err != nil {
		log.Println("图片打开失败 : ", err)
		return out, err
	}
	bounds := m.Bounds()
	dx := bounds.Dx()
	dy := bounds.Dy()
	log.Println("图片宽 ： ", dx)
	log.Println("图片高 ： ", dy)
	out.Width = dx
	out.Height = dy
	x1, x2 := GetX(m, dx, dy)
	log.Println("x = ", x1, x2)
	out.X1 = x1
	out.X2 = x2
	y1, y2 := GetY(m, dx, dy)
	log.Println("y = ", y1, y2)
	out.Y1 = y1
	out.Y2 = y2
	out.Coordinate = make([]*XY, 0, 4)
	out.Coordinate = append(out.Coordinate, &XY{X:x1, Y:y1})
	out.Coordinate = append(out.Coordinate, &XY{X:x1, Y:y2})
	out.Coordinate = append(out.Coordinate, &XY{X:x2, Y:y1})
	out.Coordinate = append(out.Coordinate, &XY{X:x2, Y:y2})
	return out, nil
}

func GetX(m image.Image, dx, dy int) (int, int) {
	tempList := make([]kv, 0)
	for i := 0; i < dx-1; i++ {
		x1 := i
		x2 := i+1
		sum := 0
		for j := 0; j < dy; j++ {
			colorRgb1 := m.At(x1, j)
			colorRgb2 := m.At(x2, j)
			r1, g1, b1 := GetRgbInt(colorRgb1)
			r2, g2, b2 := GetRgbInt(colorRgb2)
			sum = sum + abs(r1-r2) + abs(g1-g2) + abs(b1-b2)
		}
		tempList = append(tempList, kv{i, sum})
	}
	sort.Slice(tempList, func(i, j int) bool {
		return tempList[i].v > tempList[j].v
	})

	//log.Println(tempList[0])
	//log.Println(tempList[1])

	times := 0
	x1 := tempList[times]
	times ++
	x2 := tempList[times]
	for  {
		if abs(x1.k - x2.k) > 10 {
			break
		}
		times ++
		x2 = tempList[times]
	}
	if x1.k > x2.k {
		return x2.k, x1.k
	}
	return x1.k, x2.k
}

func GetY(m image.Image, dx, dy int) (int, int) {
	tempList := make([]kv, 0)
	for i := 0; i < dy-1; i++ {
		y1 := i
		y2 := i+1
		sum := 0
		for j := 0; j < dx; j++ {
			colorRgb1 := m.At(j, y1)
			colorRgb2 := m.At(j, y2)
			r1, g1, b1 := GetRgbInt(colorRgb1)
			r2, g2, b2 := GetRgbInt(colorRgb2)
			sum = sum + abs(r1-r2) + abs(g1-g2) + abs(b1-b2)
		}
		tempList = append(tempList, kv{i, sum})
	}
	sort.Slice(tempList, func(i, j int) bool {
		return tempList[i].v > tempList[j].v
	})
	//log.Println(tempList[0])
	//log.Println(tempList[1])
	//log.Println(tempList[2])
	times := 0
	y1 := tempList[times]
	times ++
	y2 := tempList[times]
	for  {
		if abs(y1.k - y2.k) > 10 {
			break
		}
		times ++
		y2 = tempList[times]
	}
	if y1.k > y2.k {
		return y2.k, y1.k
	}
	return y1.k, y2.k
}

func GetRgbInt(colorRgb color.Color) (int, int, int) {
	r, g, b, _ := colorRgb.RGBA()
	r_uint8 := uint8(r >> 8)  //转换为 255 值
	g_uint8 := uint8(g >> 8)
	b_uint8 := uint8(b >> 8)
	//a_uint8 := uint8(a >> 8)
	return int(r_uint8), int(g_uint8), int(b_uint8)
}

func abs(i int) int {
	if i < 0 {
		return -i
	}
	return i
}
